package com.ebo.kline.listener;

import android.content.Context;
import android.text.Html;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebo.commonlib.utils.AppUtil;
import com.ebo.kline.KConfig;
import com.ebo.kline.model.IKData;
import com.ebo.kline.util.KUtils;
import com.ebo.kline.view.ChartInfoView;
import com.github.mikephil.charting.charts.Chart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.ihongqiqu.util.DisplayUtils;

import java.util.List;

/**
 * Created by dell on 2017/9/28.
 */

public class InfoViewListener implements OnChartValueSelectedListenerExtend {

    protected List<? extends IKData> mList;
    protected double mLastClose;
    protected ChartInfoView mInfoView;
    protected TextView mDescView;
    protected int mWidth;
    protected KConfig mConfig;
    /**
     * if otherChart not empty, highlight will disappear after 3 second
     */
    protected Chart mOtherChart;
    private OnChartValueSelectedListenerExtend otherChartListener;


    public InfoViewListener(Context context, List<IKData> list, ChartInfoView infoView) {
        mWidth = KUtils.getWidthHeight(context)[0];
        mList = list;
        mInfoView = infoView;
    }

    public InfoViewListener(Context context, double lastClose, List<IKData> list, ChartInfoView infoView) {
        this(context, list, infoView);
        mLastClose = lastClose;
    }

    public InfoViewListener(Context context, double lastClose, List<IKData> list, ChartInfoView infoView, Chart otherChart) {
        this(context, lastClose, list, infoView);
        mOtherChart = otherChart;
    }


    public InfoViewListener(Context context, double lastClose, List<IKData> list, ChartInfoView infoView, TextView descView, Chart otherChart) {
        this(context, lastClose, list, infoView, otherChart);
        this.mDescView = descView;

    }

    public void setLastPrice(double lastPrice) {
        this.mLastClose = lastPrice;
    }

    public void setSubListener(OnChartValueSelectedListenerExtend listener) {
        otherChartListener = listener;
    }

    public void setConfig(KConfig config) {
        this.mConfig = config;
    }

    public void setData(List<? extends IKData> list) {
        this.mList = list;
    }


    @Override
    public void onValueSelected(Entry e, Highlight h) {
        int x = (int) e.getX();
        if (x < mList.size()) {
            mInfoView.setVisibility(View.VISIBLE);
            mInfoView.setData(mList.get(x).getClose_price(), mList.get(x));
            onOtherValueSelected(x);
            otherChartListener.onOtherValueSelected(x);
        }
        RelativeLayout.LayoutParams lp =new RelativeLayout.LayoutParams(mInfoView.getLayoutParams().width,mInfoView.getLayoutParams().height);
        lp.alignWithParent = true;

        if (h.getXPx() < mWidth / 2) {
            lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        } else {
            lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        }
        lp.topMargin = DisplayUtils.dip2px(AppUtil.getCompactContext(mInfoView), 16);
        mInfoView.setLayoutParams(lp);
        if (mOtherChart != null) {
            mOtherChart.highlightValues(new Highlight[]{new Highlight(h.getX(), Float.NaN, 0)});
        }
    }


    protected Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (mList.size() > 0)
                mDescView.setText(Html.fromHtml(getDesc(mList.size() - 1)));
        }
    };


    @Override
    public void onNothingSelected() {
        mInfoView.setVisibility(View.GONE);
        if (mOtherChart != null) {
            mOtherChart.highlightValues(null);
        }
        otherChartListener.onOtherNothingSelected();
    }

    protected String getDesc(int position) {
        return String.valueOf(position);
    }

    @Override
    public void onOtherValueSelected(int x) {
        if (mDescView != null && runnable != null) {
            mDescView.setText(Html.fromHtml(getDesc(x)));
            mDescView.removeCallbacks(runnable);
            mDescView.postDelayed(runnable, 2000);
        }
    }

    @Override
    public void onOtherNothingSelected() {

    }
}
